* WINDOWS PREAMBLE. 
#delimit ;


*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%;	
/*	
	
PLEASE NOTE THE CODE HAS BEEN UPDATED SINCE PUBLICATION.

To find the edited code, look in the comments below.

In the published paper, the extensive margin elasticity is 
calculated using the indirect least squares estimate. For example, 
in table 1, column 1, the extensive margin elasticity is 0.65.

The elasticity equation is 
(ln(p_t0 + beta) - ln(p_t0))/(ln(i_tw1-i_tn1)- ln(i_tw1-i_tn1))

where 
p_t0 is the mean of the outcome in the pre period in the treatment group
i_tw1 is after tax and transfer income for those who are working in the post period
i_tn1 is after tax and transfer income for those who are not working in the post period
i_tw0 is after tax and transfer income for those who are working in the pre period
i_tn1 is after tax and transfer income for those who are not working in the pre period

In the published paper, beta is the indirect least squares estimate.
The ILS estimate is calculated by dividing the reduced form 
estimate by a first stage. In table 1, that is dividing the difference-
in-difference estimate of OBRA 93 on 100% poverty by the estimate 
from a regression where the outcome is realized EITC in $1,000s. 
We find this to be an error. 

For strategies where we use diff-in-diff, beta should be the coefficient
on treat*post. Where we use the parameterized, simulated EITC,
we need to multiply the estimate by the change in simulated EITC 
for the period. 

The log file contains output to replicate both versions of the 
elasticity under the headers Original Calculation and 
Corrected Calculation. 

See Addendum for more details on this change, and reporting of 
revised elasticities compared to published elasticities.  
 
		
*/
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%;	

global rawDir "LOCATION OF RAW CPS DATA";
global dataDir "WORKING DIRECTORY";
global currDir "PROGRAM DIRECTORY";
cd "$currDir" ;
capture log close ;
clear all ;
log using 7_SingleRestrictions_analysis.log, replace text;
global beginTime "DateTime: $S_DATE $S_TIME";
set more off ;
set linesize 255 ;
set matsize 10000 ;
set mem 2g ;
cd "$dataDir" ;


****************************************************************************************************;
* ANALYSIS ;
****************************************************************************************************;

local rsig = .0001 ;




****************************************************************************************************;
* FINAL RESTRICTIONS;
****************************************************************************************************;
use singleWomen_singleRestrictions ;
count ;

drop if statefip == 2 | statefip == 15;
count ;
keep if year>=1984&year<=2013;
count ;
*keep if incdisab==0&incss==0&incssi==0 ;
count ;
keep if age>=21&age<=48;
count ;

replace age_44 = 1 if age == 48 ;
drop age_48 age_52 ;
drop childAgeBin_0_1 famtot;


****************************************************************************************************;
* VARIABLES AND LOCALS  ;
****************************************************************************************************;
drop unempr ;
gen emppop = emp/pop ;
gen unempr = (unemp/(unemp+emp)) ;
gen earnings = incwage+incbus+incfarm ;
gen children = min(qualChild,3) ;
gen byte races_1 = white ;
gen byte races_2 = hispanic ;
gen byte divorced_1 = divorced * (children >0) ;
gen byte divorced_2 = divorced * (children >1) ;
gen byte anywave_1 = anywave * (children>0) ;
gen byte anywave_2 = anywave * (children>1) ;
gen ur_1 = unempr * (children>0) ;
gen ur_2 = unempr * (children>1) ;
gen stXyr = year*1000 * state ;
qui tab year, gen(yI) ;

local familyincome "famwage fambus famfarm famss famwelfr famgov 
	famaloth famretir famssi famdrt famint famunemp famwkcom famvet famsurv 
	famdisab famdivid famrent fameduc famchild famalim famasist famother" ;
egen cpsfamilyincome = rowtotal(`familyincome') ;
gen fatti = cpsfamilyincome+stampval+heatval+houssub+schlunch-fiitax-siitax-fica ;
egen inctaxes = rowtotal(fiitax siitax fica) ;
egen famearni = rowtotal(famwage fambus famfarm) ;
egen famchildplus = rowtotal(famaloth	famalim	famchild	famother	fameduc	famasist	famsurv) ;
replace famwelfr = famwelfr + stampval+heatval ;
egen famcapitals = rowtotal(famint	famdrt	famdivid	famrent);
egen famgovern = rowtotal(famgov	famunemp	famvet	famwkcom) ;
gen famresto = fatti + inctaxes - famearni - famwelfr - famchildplus - famcapitals ;
local income_comps "fatti inctaxes famearni famwelfr famchildplus famcapitals famresto" ;
*tabstat `income_comps' [aw=wtsupp] if `medEdu', by(year) s(mean) c(v) ;
foreach fpl of numlist 25(25)500 {;
	gen byte fpl`fpl' = (cpsfamilyincome>(`fpl'/100)*fpl) ;
	gen byte afpl`fpl' = (fatti>(`fpl'/100)*fpl) ;
	local povertyvars "`povertyvars' fpl`fpl' afpl`fpl'";
	};


gen realFedEic  = fedEIC/1000 ;
gen simFedEicUn = fedEICSIV_uni/1000 ;
gen simFedEic82 = fedEICSIV_82cps/1000 ;
gen simFedEic92 = fedEICSIV_92cps/1000 ;
gen treatChld1p = (year > 1993) * (children > 0) ;
gen treatChld2p = (year > 1993) * (children > 1) ;

global wgt "wtsupp" ;
local shrtDemoFedChld1 "yI* cI* age_* hgc_* races_* divorced" ;
local shrtDemoFedChld2 "yI* cI* age_* hgc_* races_* divorced" ;

local fuUnDemoFedChld1 "`shrtDemoFedChld1' fedRegTaxSIV_uni afdcSIV_uni anywave_1 ur_1" ;
local fuUnDemoFedChld2 "`shrtDemoFedChld2' fedRegTaxSIV_uni afdcSIV_uni anywave_2 ur_2" ;
local fu82DemoFedChld1 "`shrtDemoFedChld1' fedRegTaxSIV_82cps afdcSIV_82cps anywave_1 ur_1" ;
local fu82DemoFedChld2 "`shrtDemoFedChld2' fedRegTaxSIV_82cps afdcSIV_82cps anywave_2 ur_2" ;
local fu92DemoFedChld1 "`shrtDemoFedChld1' fedRegTaxSIV_92cps afdcSIV_92cps anywave_1 ur_1" ;
local fu92DemoFedChld2 "`shrtDemoFedChld2' fedRegTaxSIV_92cps afdcSIV_92cps anywave_2 ur_2" ;

local shrtYrFed "year>=1991&year<=1998" ;
local fullYrFed "year>=1984&year<=1998" ;

local allEdu "hgc>=0";
local lowEdu "hgc<13";
local medEdu "hgc<16";
local child "children>0";

local treat1plus "children>0";
local treat2plus "children>1";

local shrtprefed "year>1990&year<1994";
local shrtpostfed "year>1993";
local fullprefed "year==1984";
local fullpostfed "year>1993";
local longprefed "year==1984";
local longpostfed "year==2013";



****************************************************************************************************;

* THE CODE BELOW HAS BEEN UPDATED SINCE PUBLICATION ;


* REGRESSIONS (ONE PARAMETER) ;
****************************************************************************************************;
program define regHelper ;
	args matrix beginRow column tgroup pre post outcome endo treatment absorb controls sample;
	di "REG HELPER - `beginRow' `column' `outcome' `treatment' `sample' `absorb' `controls' ";
	di "REDUCED FORM ";
	areg `outcome' `treatment' `controls' if `sample' [aw=$wgt], absorb(`absorb') cluster(state) ;
	mat `matrix'[`beginRow',`column'] = _b[`treatment']; 
	local redform = _b[`treatment'] ;
	mat `matrix'[`beginRow'+1,`column'] = _se[`treatment']; 
	local ts = round(_b[`treatment']/_se[`treatment'], .01) ;
	mat `matrix'[`beginRow'+5,`column'] = e(N); 
	mat `matrix'[`beginRow'+7,`column'] = e(df_r); 
	di "FIRST STAGE " ;
	areg `endo' `treatment' `controls' if `sample' [aw=$wgt], absorb(`absorb') cluster(state) ;
	mat `matrix'[`beginRow'+2,`column'] = `matrix'[`beginRow',`column'] / _b[`treatment'] ; 
	summ `outcome' if `sample' [aw=$wgt], meanonly ;
	mat `matrix'[`beginRow'+3,`column'] = `matrix'[`beginRow'+2,`column'] / r(mean) ; 
	mat `matrix'[`beginRow'+6,`column'] = r(mean) ;
	
	
* In putting this replication package together we uncovered an error ;
* Original code, use to generate the results of the paper ;	

	summ `outcome' if `sample' & `tgroup' & year>1990&year<1994 [aw=$wgt] , meanonly ;	
	local p_t0_orig = r(mean) ;
	summ atti if `sample' & `tgroup' & year>1993 & earnings>0 [aw=$wgt] , meanonly ;	
	local i_tw1_orig = r(mean) ;
	summ atti if `sample' & `tgroup' & year>1993 & earnings==0 [aw=$wgt] , meanonly ;	
	local i_tn1_orig = r(mean) ;
	summ atti if `sample' & `tgroup' & year>1990&year<1994 & earnings>0 [aw=$wgt] , meanonly ;	
	local i_tw0_orig = r(mean) ;
	summ atti if `sample' & `tgroup' & year>1990&year<1994 & earnings==0 [aw=$wgt] , meanonly ;	
	local i_tn0_orig = r(mean) ;

	local beta_orig = `matrix'[`beginRow'+2,`column'] ;
	local e1_orig = (log(`p_t0_orig'+`beta_orig')-log(`p_t0_orig')) / (log(`i_tw1_orig'-`i_tn1_orig')-log(`i_tw0_orig'-`i_tn0_orig')) ;
* Original code end ;	
	
* Corrected code ;

	summ `outcome' if `sample' & `tgroup' & `pre' [aw=$wgt] , meanonly ;	
	local p_t0 = r(mean) ;
	summ atti if `sample' & `tgroup' & `post' & earnings>0 [aw=$wgt] , meanonly ;	
	local i_tw1 = r(mean) ;
	summ atti if `sample' & `tgroup' & `post' & earnings==0 [aw=$wgt] , meanonly ;	
	local i_tn1 = r(mean) ;
	summ atti if `sample' & `tgroup' & `pre' & earnings>0 [aw=$wgt] , meanonly ;	
	local i_tw0 = r(mean) ;
	summ atti if `sample' & `tgroup' & `pre' & earnings==0 [aw=$wgt] , meanonly ;	
	local i_tn0 = r(mean) ;
	
	gen temptreat = `tgroup'  ;
	summ `treatment' if `sample' & temptreat==1 & `post', meanonly;
	local simeitc_treat_post = r(mean) ;
	summ `treatment' if `sample' & temptreat==1 & `pre', meanonly ;
	local simeitc_treat_pre = r(mean) ;
	summ `treatment' if `sample' & temptreat==0 & `post', meanonly ;
	local simeitc_control_post = r(mean) ;
	summ `treatment' if `sample' & temptreat==0 & `pre', meanonly;
	local simeitc_control_pre = r(mean) ;	
	local simeitc_dnd = ((`simeitc_treat_post'-`simeitc_treat_pre')-(`simeitc_control_post'-`simeitc_control_pre')) ;
	local beta =  `simeitc_dnd' * `redform' ;
	drop temptreat ;
	
	local e1 = (log(`p_t0'+`beta')-log(`p_t0')) / (log(`i_tw1'-`i_tn1')-log(`i_tw0'-`i_tn0')) ;
	mat `matrix'[`beginRow'+4,`column'] = `e1' ; 
	
* Corrected code end ;	
	
	foreach s in e1 p_t0 p_t0_orig beta e1_orig beta_orig simeitc_treat_post simeitc_treat_pre simeitc_control_post simeitc_control_pre simeitc_dnd redform {;
		local `s' = round(``s'',.001) ;
		};
	foreach s in i_tw1 i_tn1 i_tw0 i_tn0 i_tw1_orig i_tn1_orig i_tw0_orig i_tn0_orig {;
		local `s' = round(``s'',1) ;
		};
		
	mat `matrix'[`beginRow'+8,`column'] = `p_t0' ;
	mat `matrix'[`beginRow'+9,`column'] = `i_tw0';
	mat `matrix'[`beginRow'+10,`column'] = `i_tn0';
	mat `matrix'[`beginRow'+11,`column'] = `i_tw1';
	mat `matrix'[`beginRow'+12,`column'] = `i_tn1';
	
	di "OUTPUT ELASTICITY CALCULATION ********* " ;
	di " " ;
	di " " ;
	di "ORIGINAL CALCULATION **************";
	di "  Mean of Outcome - `outcome' - in the pre-period `p_t0_orig'" ;
	di "      used in elasticity numerator" ;
	di " " ;
	di "  After Tax and Transfer Income for Elasticity Denominator" ;
	di "    ATTI, pre-period, working        `i_tw0_orig' ";
	di "    ATTI, pre-period, not working    `i_tn0_orig' ";
	di "    ATTI, post-period, working       `i_tw1_orig' ";
	di "    ATTI, post-period, not working   `i_tn1_orig' ";
	di " " ;
	di "  ILS estimate `beta_orig'" ;
	di "  Elasticity `e1_orig'" ;
	di " " ;
	di " " ;
	di "CORRECTED CALCULATION ***************** ";
	di "  Mean of Outcome - `outcome' - in the pre-period `p_t0'" ;
	di "      used in elasticity numerator" ;
	di " " ;
	di "  After Tax and Transfer Income for Elasticity Denominator" ;
	di "    ATTI, pre-period, working        `i_tw0' ";
	di "    ATTI, pre-period, not working    `i_tn0' ";
	di "    ATTI, post-period, working       `i_tw1' ";
	di "    ATTI, post-period, not working   `i_tn1' ";
	di " " ;	
	di "  Beta on treat*post or simulated EITC `redform'" ;
	di "  Amount simulated EITC changed due to policy `simeitc_dnd'" ;
	di "  Note that above amount is 1 for DnD specifications" ;
	di "  Values used to calculate simulated EITC change " ;
	di "    Treated group, post period  `simeitc_treat_post'";
	di "    Treated group, pre period  `simeitc_treat_pre'";
	di "    Control group, post period  `simeitc_control_post'";
	di "    Control group, pre period  `simeitc_control_pre'";
	di "    Note that above four calculations are 0s and 1s for the DnD specifications "; 
	di "  Final estimate to be used in numerator of elasticity calculation `beta' " ;
	di "  Elasticity `e1'" ;
	
	end ;




*-------------------------------------------------------------------------;
* Appendix Table 5: Relaxing Restrictions (Difference-in-Difference Estimates of OBRA93 on ATT Income Above 100% of the Federal Poverty Threshold) ;
*-------------------------------------------------------------------------;
local outcome "afpl100" ;
mat r = J(13,8,.z) ;
regHelper "r" "  1" "3" "`treat1plus'" "`shrtprefed'" "`shrtpostfed'" "`outcome'" "realFedEic" "treatChld1p" "stXyr" "`shrtDemoFedChld1'" "`shrtYrFed' & `medEdu' & (whynwly!=2 & whynwly!=4)" ;
regHelper "r" "  1" "4" "`treat1plus'" "`shrtprefed'" "`shrtpostfed'" "`outcome'" "realFedEic" "treatChld1p" "stXyr" "`fu82DemoFedChld1'" "`shrtYrFed' & `medEdu' & (whynwly!=2 & whynwly!=4)" ;
regHelper "r" "  1" "5" "`treat1plus'" "`shrtprefed'" "`shrtpostfed'" "`outcome'" "realFedEic" "treatChld1p" "stXyr" "`shrtDemoFedChld1'" "`shrtYrFed' & `medEdu' & age>23" ;
regHelper "r" "  1" "6" "`treat1plus'" "`shrtprefed'" "`shrtpostfed'" "`outcome'" "realFedEic" "treatChld1p" "stXyr" "`fu82DemoFedChld1'" "`shrtYrFed' & `medEdu' & age>23" ;
regHelper "r" "  1" "7" "`treat1plus'" "`shrtprefed'" "`shrtpostfed'" "`outcome'" "realFedEic" "treatChld1p" "stXyr" "`shrtDemoFedChld1'" "`shrtYrFed' & `lowEdu' & (whynwly!=2 & whynwly!=4) & age>23" ;
regHelper "r" "  1" "8" "`treat1plus'" "`shrtprefed'" "`shrtpostfed'" "`outcome'" "realFedEic" "treatChld1p" "stXyr" "`fu82DemoFedChld1'" "`shrtYrFed' & `lowEdu' & (whynwly!=2 & whynwly!=4) & age>23" ;
mat list r, nodotz ;

	
*/
*;
log close ;























